package com.siriframe.cloud.home.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.siriframe.cloud.home.feign.SkuFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@Slf4j
public class HomeController {

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private SkuFeignService skuFeignService;

    @GetMapping("/getSkuInfo1")
    public String getSkuInfo1() {
        String url = "http://localhost:8091/test";
        String result = restTemplate.getForObject(url, String.class);
        return result;
    }

    @GetMapping("/getSkuInfo2")
    public String getSkuInfo2() {
        String url = "http://SIRI-CLOUD-SKU/test";
        String result = restTemplate.getForObject(url, String.class);
        return result;
    }

    @GetMapping("/getSkuInfo3")
    public String getSkuInfo3() {
        String result = skuFeignService.test();
        return result;
    }

    @GetMapping("/getSkuInfoTimeout")
    public String getSkuInfoTimeout() {
        return skuFeignService.testTimeout();
    }

    @GetMapping("/getHystrix")
    public String getHystrix() {
        return skuFeignService.testHystrix();
    }

    @GetMapping("/getHystrixTimeout")
    @HystrixCommand(fallbackMethod = "testHystrixTimeoutHandler",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "500")
    })
    public String getHystrixTimeout() {
        return skuFeignService.testHystrixTimeout();
    }

    public String testHystrixTimeoutHandler(){
        return "接口降级啦！！！!";
    }
}
